﻿// This file is part of GEOTRON.

// GEOTRON is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The GEOTRON library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with GEOTRON.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;

namespace Geotron
{
    public static partial class GeoResolve
    {
        /// <summary>
        /// List of available countries which can be resolved.
        /// </summary>
        static List<Country> Countries;

        /// <summary>
        /// Contains the pieces of the address.
        /// </summary>
        static List<string> AddressPieces;

        /// <summary> Stores the result of the postcode location. </summary>
        static LocationResult PostcodeResult;

        /// <summary>
        /// Attempt to resolve the given address to a set of latitude and longitude numbers.
        /// </summary>
        /// <param name="address">The address to be resolved.</param>
        static public LocationResult Resolve(string Address)
        {
            // Split and replace the address
            string[] AddressArray = Address.Split(',');

            AddressPieces = new List<string>();
            
            for (int i = 0; i < AddressArray.Count(); i++)
            {
                string AddressLine = AddressArray[i].Trim();
                AddressLine = AddressLine.ToUpper();
                AddressPieces.Add(AddressLine);
            }

            List<Country> Possibilities = new List<Country>();
            for (int i = 0; i < Countries.Count; i++)
            {
                // Assumption: Address was composed of meaningless data, meaningless data, p;ostcode [last item]
                // Run a check to see if this is a postcode, and if so then add the country to a list of possibilities.
                if (Countries[i].isThisAPostcode(AddressPieces[AddressPieces.Count - 1]) == true)
                {
                    Possibilities.Add(Countries[i]);
                    Trace.WriteLine(Countries[i].Name + " is a possible location.");

                    PostcodeResult = TryResolvePostcode(AddressPieces[AddressPieces.Count - 1], Countries[i]);
                    return PostcodeResult;
                }

                else
                {
                    Trace.WriteLine("The end item is not a postcode, therefore there is no more than can be done to resolve this address!");
                    return new LocationResult(Result.CannotResolve, 0);
                } 
            }

            return new LocationResult(Result.CannotResolve, 0);
        }
    }
}
